package weaver.interfaces.jjy.duss.hr;

import com.sap.mw.jco.JCO;
import weaver.interfaces.jjy.duss.util.SAPConnPool;

import java.io.*;

/**
 * @program: ecology-9-demo
 * @description: ${description}
 * @author: Dusongsong
 * @create: 2019-12-17
 */
public class ReceivePhoto {

    JCO.Client myConnection = null;
    JCO.Function bapi = null;
    JCO.ParameterList input = null;
    JCO.ParameterList inputtable = null;

    public String execute() throws Exception {


        try {
            SAPConnPool SAPConn = new SAPConnPool();
            myConnection = SAPConn.getConnection();
            bapi = SAPConn.excuteBapi("ZFM_HR_PERNR");
            input = bapi.getImportParameterList();//输入参数和结构处理
            inputtable = bapi.getTableParameterList();//输入表的处理
        } catch (Exception e) {
            e.printStackTrace();
        }
        //入参为字段
        //入参为字段
        input.setValue("SEL", "JIEKNO");                   //HR接口标识：离职
        input.setValue("00000005", "IV_PERNR");

        //执行函数
        myConnection.execute(bapi);
        JCO.ParameterList out = bapi.getExportParameterList();//输出参数和结构处理
        JCO.ParameterList Table00 = bapi.getTableParameterList();//输出表的处理

        System.out.println("输出的字段：EV_PERNR：---" + out.getValue("EV_PERNR"));
        JCO.Table tabs = Table00.getTable("PHOTO");
        System.out.println(tabs.getNumRows());
        byte[] lines = null;
        for(int i = 0;i<tabs.getNumRows();i++){
            tabs.setRow(i);
            byte[] line = (byte[])tabs.getValue("LINE");
            if(lines==null){
                lines=line;
            }else {
                lines= byteMerger(lines,line);
            }
        }
        System.out.println(lines.length);
        String dest = "/app/weaver/ecology/filesystem/photo/"+"IV_PERNR"+".jpg";
        System.out.println("LINE=" + tabs.getValue("LINE"));
        return dest;
    }

    public static byte[] byteMerger(byte[] bt1, byte[] bt2){
        byte[] bt3 = new byte[bt1.length+bt2.length];
        int i=0;
        for(byte bt: bt1){
            bt3[i]=bt;
            i++;
        }
        for(byte bt: bt2){
            bt3[i]=bt;
            i++;
        }
        return bt3;
    }

    public static void main(String[] args) throws Exception {
        ReceivePhoto testAction = new ReceivePhoto();
        testAction.execute();
    }
    public static void byteArrayToFile(byte[] src, String filePath) {

        File dest = new File(filePath);//新文件
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new ByteArrayInputStream(src);//字节数组
            os = new FileOutputStream(dest);//输出

            //一样的字节数组缓冲操作
            byte[] flush = new byte[1024 * 10];
            int len = -1;
            while ((len = is.read(flush)) != -1) {
                os.write(flush, 0, len);
            }
            os.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != os) {//关闭文件流
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

    


